package com.example.gw_back_end.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.gw_back_end.domain.PageParam;
import com.example.gw_back_end.domain.PageResult;
import com.example.gw_back_end.domain.SearchParams;
import com.example.gw_back_end.domain.entities.Category;
import com.example.gw_back_end.mapper.CategoryMapper;
import com.example.gw_back_end.service.CategoryService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    private final CategoryMapper categoryMapper;
    public CategoryServiceImpl(CategoryMapper categoryMapper) {
        this.categoryMapper = categoryMapper;
    }

    // 获取游戏类别列表
    @Override
    public PageResult<Category> getCategoryList(PageParam pageParam) {
        if (pageParam.getPage().equals(-1)) {
            // 查询全部数据，不分页
            List<Category> list = categoryMapper.selectList(null);
            Page<Category> categoryPage = new Page<>();
            categoryPage.setRecords(list);
            categoryPage.setTotal(list.size());
            categoryPage.setSize(list.size());
            categoryPage.setCurrent(1L);
            return PageResult.setPageResult(categoryPage);
        } else {
            // 分页查询
            Page<Category> categoryPage = categoryMapper.selectPage(
                    new Page<>(pageParam.getPage(), pageParam.getSize()), null
            );
            return PageResult.setPageResult(categoryPage);
        }
    }

    // 新增游戏类别
    @Override
    public boolean addCategory(Category category) {
        return categoryMapper.insert(category) > 0;
    }

    // 删除游戏类别
    @Override
    public boolean deleteCategory(List<Integer> ids) {
        return categoryMapper.deleteByIds(ids) > 0;
    }

    // 获取游戏类别
    @Override
    public Category getCategoryById(Integer id) {
        return categoryMapper.selectById(id);
    }

    // 更新游戏类别
    @Override
    public boolean updateCategory(Category category) {
        return categoryMapper.updateById(category) > 0;
    }

    @Override
    public PageResult<Category> searchCategory(SearchParams searchParams) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Category::getCategoryName,searchParams.getKeyword());
        Page<Category> categoryPage = categoryMapper.selectPage(new Page<>(searchParams.getPageParam().getPage(), searchParams.getPageParam().getSize()), queryWrapper);
        return PageResult.setPageResult(categoryPage);
    }
}
